import path from "path";
import fs from "fs";
import _ from "lodash";

import { transform, getWords, formatKey } from "../lib/transform";


import {
  InitStructure,
  ModifyJSON,
  deleteFolderSync,
  WriteFile,
} from "../lib/file";
declare interface Config {
  languages: Array<string>; //语言库
  keys: {
    [key: string]: string; //翻译文本:对应键名
  };
}
declare interface PluginsParms {
  languages: Array<string>; //语种类型集合
  language: string; //当前语言
  path: string; //项目地址
  appId: string; //应用id
  key: string; //应用密钥
  folderName: string; //资源文件夹名
}
/**
 * vite文件操作(创建目录&生成翻译)
 */
function AutoPlugins(params: PluginsParms) {
  let config_path: string = path.join(__dirname, "../../plugin_config.json");
  let rootPath: string;
  let ProjectInit;
  //统计文件刷新次数
  let count = 0;
  if (!params.languages) params.languages = ["zh", "en"];
  if (!params.language) params.language = "zh";
  if (!params.folderName) params.folderName = "languages";
  return {
    name: "lange-auto-js",
    config(config) {
      console.log('config')

      let IndexFile = fs
        .readFileSync(path.join(__dirname, "../../template/vue.js"))
        .toString();

      rootPath = path.join(
        fs.existsSync(params.path) ? "" : config.root,
        params.path,
        params.folderName
      );
      config.optimizeDeps = config.optimizeDeps || {};
      config.optimizeDeps.include = [
        `${rootPath}/modules`,
        ...(config.optimizeDeps.include||[]),
      ];
      ProjectInit = new InitStructure({
        path: rootPath,
        IndexFile,
        languages: params.languages,
        language: params.language,
        config_path,
      });
      ProjectInit.inteData();
      return config
    },
    transform: async function(code?: string, id?: string) {
      let words = getWords(code);
      if (words.length) {
        let arr1 = formatKey(words);
        let arr2 = formatKey(ProjectInit.getKeys());
        //差集查询 (新单词)
        let differenceBy = _.differenceBy(arr1, arr2, "name");
        let tasks = await transform(differenceBy, params);
        for (let i = 0, len = tasks.length; i < len; i++) {
          let task = tasks[i];
          let filePath = path.join(
            rootPath,
            "modules",
            task.language + ".json"
          );
          let data: any = fs.readFileSync(filePath).toString();
          data = JSON.parse(data);
          task.list.forEach((item) => {
            data[item.name] = item.value;
          });
          WriteFile(filePath, JSON.stringify(data, null, 2));
        }
      }
      return code;
    },
  };
}

export { AutoPlugins };
export const name="插件"

// plugin.buildStart().then(()=>{
//   plugin.transform(`
//     <h1>{{$t("你好")}}</h1>
//     <h1>{{$t("登录")}}</h1>
//     <h1>{{$t("首页")}}</h1>
//     <h1>{{$t("我的")}}</h1>
//     <h1>{{$t("钱包")}}</h1>
//     <h1>{{$t("宝贝")}}</h1>
//     <h1>{{$t("钩子")}}</h1>
//     <h1>{{$t("再见")}}</h1>
//     <h1>{{$t("木头")}}</h1>
//     <h1>{{$t("日本")}}</h1>
//     <h1>{{$t("中国")}}</h1>
//     <h1>{{$t("德国")}}</h1>
//     <h1>{{$t("狗")}}</h1>
//     <h1>{{$t("猪")}}</h1>
//   `);
// });
